package Dao;

import Model.Employee;
import java.sql.*;
import javax.swing.JOptionPane;

public class LoginDao {
   // private DatabaseConnect dbConnect;
   Connection conn = null;
   ResultSet rs = null;
   PreparedStatement pst = null;
    
    //constructor
    public LoginDao(){
        conn = DatabaseConnect.ConnectDb();
    }
    
    //if user in employee table return Employee
    public Employee isUserAuthorized(String userName, String password){
        String sql = "select * from Employee where Username=? and Password=?";
        try{        
            pst = conn.prepareStatement(sql);
            pst.setString(1, userName);
            pst.setString(2, password);
        
            rs = pst.executeQuery();
        
            if(rs.next()){
                JOptionPane.showMessageDialog(null, "Username and Password is correct");
                return (new Employee( rs.getInt("SSN"), rs.getString("Name"),
                                      rs.getInt("Age"), rs.getString("Gender"),
                                      rs.getInt("Classification")
                                    )                        
                        );                        
            }        
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "LoginDao fault!!");
            return null;
        }
        return null;
    }
    
}
